package io.renren.modules.com.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoleilu.hutool.util.CollectionUtil;
import com.xiaoleilu.hutool.util.StrUtil;

import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.com.entity.ComAssetEntity;
import io.renren.modules.com.service.ComAssetService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysBuildEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysBuildService;
import io.renren.modules.sys.service.SysUserService;

/**
 * 
 * 
 * @author pfp
 * @email
 * @date 2017-07-15 10:33:34
 */
@RestController
@RequestMapping("asset")
public class ComAssetController extends AbstractController {

	@Autowired
	private ComAssetService comAssetService;

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private SysBuildService sysBuildService;

	/**
	 * 列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/list")
	@RequiresPermissions("com:asset:list")
	public R list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		String rentUser = (String) params.get("rentUser");
		String rentStatus = (String) params.get("rentStatus");
		if (StrUtil.isNotBlank(rentUser) && StrUtil.equals("0", rentStatus)) {
			int total = 0;
			PageUtils pageUtil = new PageUtils(CollectionUtil.newArrayList(), total, query.getLimit(), query.getPage());
			return R.ok().put("page", pageUtil);
		}
		List<ComAssetEntity> comAssetList = comAssetService.queryList(query);
		int total = comAssetService.queryTotal(query);
		PageUtils pageUtil = new PageUtils(comAssetList, total, query.getLimit(), query.getPage());
		List<ComAssetEntity> list = (List<ComAssetEntity>) pageUtil.getList();
		for (ComAssetEntity sysBuildEntity : list) {
			SysUserEntity user = sysUserService.queryObject(sysBuildEntity.getCreateUser());
			if (user != null) {
				sysBuildEntity.setCreateUserName(user.getRealName());
			}
			SysBuildEntity build = sysBuildService.queryObject(sysBuildEntity.getBuildId());
			if (build != null) {
				sysBuildEntity.setBuildName(build.getBuildName());
			}
		}
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{assetId}")
	@RequiresPermissions("com:asset:info")
	public R info(@PathVariable("assetId") Long assetId) {
		ComAssetEntity comAsset = comAssetService.queryObject(assetId);
		return R.ok().put("comAsset", comAsset);
	}

	@RequestMapping("/select")
	public R select(HttpServletRequest request) {
		String roomNum = request.getParameter("roomNum");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("roomNum", roomNum);
		// 查询列表数据
		List<ComAssetEntity> menuList = comAssetService.queryList(params);
		return R.ok().put("menuList", menuList);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("com:asset:save")
	public R save(@RequestBody ComAssetEntity comAsset) {
		Set<ConstraintViolation<Object>> constraintViolations = validator.validate(comAsset);
		Iterator<ConstraintViolation<Object>> iter = constraintViolations.iterator();
		while (iter.hasNext()) {
			String message = iter.next().getMessage();
			return R.error(message);
		}
		comAsset.setCreateUser(getUserId());
		comAsset.setCreateTime(new Date());
		comAsset.setRentStatus(Constant.RENT_STATUS_0);
		R r = comAssetService.save(comAsset);
		if ((Integer) r.get("code") != 0) {
			return r;
		}
		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("com:asset:update")
	public R update(@RequestBody ComAssetEntity comAsset) {
		Set<ConstraintViolation<Object>> constraintViolations = validator.validate(comAsset);
		Iterator<ConstraintViolation<Object>> iter = constraintViolations.iterator();
		while (iter.hasNext()) {
			String message = iter.next().getMessage();
			return R.error(message);
		}
		R r = comAssetService.update(comAsset);
		return r;
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("com:asset:delete")
	public R delete(@RequestBody Long[] assetIds) {
		comAssetService.deleteBatch(assetIds);
		return R.ok();
	}

}
